package com.example.finalhomework.thirdhomework

import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

// TimerLifecycleObserver 类实现了 DefaultLifecycleObserver 接口
// 用于在生命周期事件发生时控制计时器的状态
class TimerLifecycleObserver(private val viewModel: FifthViewModel) : DefaultLifecycleObserver {

    // 当生命周期所有者进入暂停状态时调用
    override fun onPause(owner: LifecycleOwner) {
        super.onPause(owner)
        viewModel.pauseTimer() // 暂停计时器
        Log.d("TimerLifecycleObserver", "Lifecycle paused, timer paused")
    }

    // 当生命周期所有者进入恢复状态时调用
    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        viewModel.resumeTimer() // 恢复计时器
        Log.d("TimerLifecycleObserver", "Lifecycle resumed, timer resumed")
    }
}